﻿using System;
using System.Globalization;
using System.Windows.Data;
using WcfService1.DTO;
using System.Linq;

namespace Lib.Converter
{
    public class SupplierConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                return ((dynamic)value).SupplierName + ":" + ((dynamic)value).SupplierId ;
            }
            else
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            var text = value.ToString().Split(new string[] {":"},StringSplitOptions.None);
            return new SupplierDTO() {SupplierName = text.FirstOrDefault() ,SupplierId = text.LastOrDefault()};
        }

        #endregion
    }
}